package service

import (
	"api/app/auth/models"
	"api/app/auth/service"
	"api/libs/api"
	"gitee.com/dreamwood/ez"
	"gitee.com/dreamwood/ez/server"
)

type UserServiceApi struct {
	api.BaseApi
}

func init() {
	c := new(UserServiceApi)
	//c.RoutePrefix = "/auth/UserService"
	c.AddRoute("/auth/UserService/getUsers", c.GetUsersAction)
}

type getUserParam struct {
	Depts []int `json:"depts"`
	Jobs  []int `json:"jobs"`
	Roles []int `json:"roles"`
	Users []int `json:"users"`
	IsAll bool  `json:"isAll"`
}

func (c *UserServiceApi) GetUsersAction(operation *server.Operation) {
	p := new(getUserParam)
	this := c.TakeOver(operation)
	if this.Try(this.FillJson(&p)) {
		return
	}
	users := make([]*models.User, 0)
	if p.IsAll {
		crud := models.NewUserCrud()
		users, _ = crud.FindBy(ez.M{}, nil, 0, 0)
	} else {

		if len(p.Depts) > 0 {
			users = append(users, service.GetUserByDepartments(p.Depts)...)
		}
		if len(p.Jobs) > 0 {
			users = append(users, service.GetUserByJobs(p.Jobs)...)
		}
		if len(p.Roles) > 0 {
			users = append(users, service.GetUserByJobs(p.Roles)...)
		}
		if len(p.Users) > 0 {
			users = append(users, service.GetUserByIds(p.Users)...)
		}
		users = service.UserDistinct(users)
	}
	//todo 精简用户数据，减少传输消耗
	this.ReturnSuccess("OK", users)
}
